﻿/* 
	PureMVC CSharp / WPF / EmployeeAdmin Demo - Login
	By Andy Adamczak <andy.adamczak@puremvc.org>
	Copyright(c) 2009 Andy Adamczak, Some rights reserved.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

using PureMVC.Patterns;
using PureMVC.Interfaces;

using GEF2.Controller;
using GEF2.Model.Storage;

namespace GEF2
{
	public class ApplicationFacade : Facade
	{
		#region Notification name constants
        public const string NAME = "ApplicationFacade";
		public const string STARTUP	    = NAME+ ".startup";
        public const string ADD_ENTITY  = NAME+ ".newEntity";
        public const string DELETE_ENTITY = NAME + ".deleteEntity";

        public const string ENTITY_ADDED = NAME+ ".entityAdded";
        public const string ENTITY_DELETED = NAME + ".entityDeleted";
        public List<Entity> mRootModel;
		
		#endregion

		#region Accessors

		/// <summary>
		/// Facade Singleton Factory method.  This method is thread safe.
		/// </summary>
		public new static IFacade Instance
		{
			get
			{
				if (m_instance == null)
				{
					lock (m_staticSyncRoot)
					{
						if (m_instance == null) m_instance = new ApplicationFacade();
					}
				}

				return m_instance;
			}
		}

		#endregion

		#region Public methods

		/// <summary>
		/// Start the application
		/// </summary>
		/// <param name="app"></param>
		public void Startup(object app)
		{
			SendNotification(STARTUP, app);

            mRootModel = new List<Entity> ();
		}

		#endregion

		#region Protected & Internal Methods

		protected ApplicationFacade()
		{
			// Protected constructor.
		}

		/// <summary>
		/// Explicit static constructor to tell C# compiler 
		/// not to mark type as beforefieldinit
		///</summary>
		static ApplicationFacade()
		{
		}

		/// <summary>
		/// Register Commands with the Controller
		/// </summary>
		protected override void InitializeController()
		{
			base.InitializeController();
			RegisterCommand(STARTUP, typeof(StartupCommand));
            RegisterCommand(ADD_ENTITY, typeof(AddEntityCommand));
            RegisterCommand(DELETE_ENTITY, typeof(DeleteEntityCommand));
        }

		#endregion

        #region extra logging
        public override void SendNotification(string notificationName)
        {
            Debug.WriteLine(string.Format("Send notification 1: {0}", notificationName));
            NotifyObservers(new Notification(notificationName));
        }
        public override void SendNotification(string notificationName, object body)
        {
            Debug.WriteLine(string.Format("Send notification 2: {0}", notificationName));
            NotifyObservers(new Notification(notificationName, body));
        }


        public override void SendNotification(string notificationName, object body, string type)
        {
            Debug.WriteLine(string.Format("Send notification 3: {0}", notificationName));
            NotifyObservers(new Notification(notificationName, body, type));
        }
        #endregion
    }
}
